package com.yyplatform.admin.platform.generator.service.impl;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import com.yyplatform.admin.platform.generator.model.dto.TableConfigDTO;
import com.yyplatform.admin.platform.generator.model.dto.TableConfigFormDTO;
import com.yyplatform.admin.platform.generator.model.dto.TableConfigQueryDTO;
import com.yyplatform.admin.platform.generator.model.entity.GenColumnConfigEntity;
import com.yyplatform.admin.platform.generator.model.entity.GenTableConfigEntity;
import com.yyplatform.admin.platform.generator.helper.MySQLSchemaHelper;
import com.yyplatform.admin.platform.generator.model.TableVO;
import com.yyplatform.admin.platform.generator.repository.GenColumnConfigRepository;
import com.yyplatform.admin.platform.generator.repository.GenTableConfigRepository;
import com.yyplatform.admin.platform.generator.service.TableConfigService;
import com.yyplatform.admin.platform.generator.utils.NameUtils;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2024-09-23
 */
@Slf4j
@AllArgsConstructor
@Service
public class TableConfigServiceImpl implements TableConfigService {

    private GenTableConfigRepository tableConfigRepository;
    private GenColumnConfigRepository columnConfigRepository;
    private ModelMapper modelMapper;

    private MySQLSchemaHelper mySQLSchemaHelper;


    @Override
    public Page<TableConfigDTO> query(TableConfigQueryDTO pageQueryDTO) {
        Page<GenTableConfigEntity> entityPage =
                tableConfigRepository.findPage(
                        pageQueryDTO.getTableName(),
                        pageQueryDTO.getPageRequestOrderByIdDesc());


        long total = tableConfigRepository.count();


        List<TableConfigDTO> list = entityPage.stream()
                .map(e -> modelMapper.map(e, TableConfigDTO.class))
                .toList();

        return new PageImpl<>(list, entityPage.getPageable(), entityPage.getTotalElements());
    }

    @Override
    public TableConfigDTO get(Long id) {
        GenTableConfigEntity entity = tableConfigRepository.getReferenceById(id);
        TableConfigDTO dto = null;
        if (entity != null) {
            dto = modelMapper.map(entity, TableConfigDTO.class);


            if (StringUtils.isEmpty(dto.getAuthor())) {
                dto.setAuthor("sleepforests");
            }

            if (StringUtils.isEmpty(dto.getTableName())) {
                dto.setTableName(entity.getTableName());
            }

            if (StringUtils.isEmpty(dto.getEntityName())) {
                dto.setEntityName(NameUtils.toPascalCase(entity.getTableName()));
            }

            if (StringUtils.isEmpty(dto.getPackageName())) {
                dto.setPackageName("com.yyplatform.admin.modules");
            }
        } else {
            dto = new TableConfigDTO();
            dto.setAuthor("sleepforests");
            dto.setTableName(entity.getTableName());
            dto.setEntityName(NameUtils.toPascalCase(entity.getTableName()));
            dto.setPackageName("com.yyplatform.admin.modules");
        }


        return dto;
    }

    @Override
    public void save(TableConfigFormDTO formDTO) {
        GenTableConfigEntity entity = modelMapper.map(formDTO, GenTableConfigEntity.class);
        tableConfigRepository.save(entity);
    }

    @Override
    public void update(TableConfigFormDTO formDTO) {

        GenTableConfigEntity entity = tableConfigRepository.getReferenceById(formDTO.getId());

        entity.setModuleName(formDTO.getModuleName());
        entity.setPackageName(formDTO.getPackageName());
        entity.setBusinessName(formDTO.getBusinessName());
        entity.setEntityName(formDTO.getEntityName());
        entity.setAuthor(formDTO.getAuthor());
        entity.setMenuParentId(formDTO.getMenuParentId());

        entity.setRoleName(formDTO.getRoleName());
        entity.setTagPrefix(formDTO.getTagPrefix());
        entity.setUrlPrefix(formDTO.getUrlPrefix());

        tableConfigRepository.save(entity);
    }

    @Override
    public void delete(Long id) {
        tableConfigRepository.deleteById(id);
        List<GenColumnConfigEntity> entityList = columnConfigRepository.findByTableConfigId(id);
        columnConfigRepository.deleteAll(entityList);
    }


    @Override
    public void resetAll(Long id) {

        List<GenColumnConfigEntity> entityList = columnConfigRepository.findByTableConfigId(id);
        columnConfigRepository.deleteAll(entityList);
    }

    @Override
    public void initAllTable() {
        List<TableVO> tableVOList = mySQLSchemaHelper.listTable();
        Map<String, TableVO> map1 =
                tableVOList.stream().collect(Collectors.toMap(TableVO::getTableName, Function.identity()));

        List<GenTableConfigEntity> entityList = tableConfigRepository.findAll();
        Map<String, GenTableConfigEntity> map2 =
                entityList.stream().collect(Collectors.toMap(GenTableConfigEntity::getTableName, Function.identity()));


        // toADD
        for (TableVO tableVO : tableVOList) {
            if (!map2.containsKey(tableVO.getTableName())) {
                GenTableConfigEntity entity = new GenTableConfigEntity();
                entity.setTableName(tableVO.getTableName());
                entity.setModuleName(tableVO.getTableComment());


                entity.setModuleName("");
                entity.setPackageName("");
                entity.setMenuParentId(0L);


                tableConfigRepository.save(entity);
            }
        }

    }

}
